package com.wolfpire.chapter.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.chapter.dao.ChapterDao;
import com.wolfpire.chapter.dao.SubjectChapterRelationDao;
import com.wolfpire.chapter.service.ChapterService;
import com.wolfpire.knowledge.dao.KnowledgeDao;
import com.wolfpire.knowledge.dao.SubjectKnowledgeRelationDao;
import com.wolfpire.knowledge.service.KnowledgeService;
import com.wolfpire.profession.dao.ProfessionDao;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.subject.dao.SubjectDao;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.MD5;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.dao.AuthorityDao;
import com.wolfpire.system.dao.OrgDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.dao.RoleDao;
import com.wolfpire.system.dao.UserDao;
import com.wolfpire.system.dao.UserRoleRelationDao;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.Chapter;
import com.wolfpire.system.model.DataDict;
import com.wolfpire.system.model.Knowledge;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.SubjectChapterRelation;
import com.wolfpire.system.model.SubjectKnowledgeRelation;
import com.wolfpire.system.model.User;
import com.wolfpire.system.model.UserRoleRelation;
import com.wolfpire.system.service.UserService;

@Service("chapterService")
public class ChapterServiceImpl extends BaseHibernateService<Chapter, Long> implements
	ChapterService {
	
	@Resource private ChapterDao chapterDao;
	@Resource private SubjectDao subjectDao;
	@Resource private ProfessionDao professionDao;
	@Resource private SubjectChapterRelationDao subjectChapterRelationDao;

	public List<Chapter> list(Chapter entity) {
		// TODO Auto-generated method stub
		return this.chapterDao.list(entity);
	}

	public Chapter getByName(String name) {
		// TODO Auto-generated method stub
		return this.chapterDao.getByName(name);
	}

	public Page<Chapter> setPageDataList(Page<Chapter> page,
			Chapter filterChapter) {
		// TODO Auto-generated method stub
		return this.chapterDao.findChapters(page, filterChapter);
	}

	public List<Chapter> getChaptersBySubjectId(Long subjectId) {
		// TODO Auto-generated method stub
		return null;
	}

	public Chapter getCompleteChapter(Long id) {
		Chapter chapter = this.get(id);
		// 绑定专业
		SubjectChapterRelation sc = this.subjectChapterRelationDao.getSCRelation(id);
		chapter.setSubjectId(sc.getSubjectId());
		Subject subject = this.subjectDao.get(sc.getSubjectId());
		/*if (null != subject) {
			knowledge.setName(subject.getName());
		}*/
		return chapter;
	}
	
	public void saveOrUpdate(Chapter chapter) {
		if (null == chapter.getId()) {
			this.chapterDao.saveOrUpdate(chapter);
		} else {
			this.chapterDao.merge(chapter);
		} 
		
		Long chapterId = chapter.getId();
		Long subjectId = chapter.getSubjectId();
		this.subjectChapterRelationDao.relate(subjectId, chapterId);
	}

	@Override
	protected IBaseHibernateDao<Chapter, Long> getEntityDao() {
		// TODO Auto-generated method stub
		return this.chapterDao;
	}

	public List<Map<String, Object>> bulidTree(Chapter chapter) {
		Profession profession = new Profession();
		List<Profession> pList = this.professionDao.list(profession );
		List<Map<String, Object>> subjects = this.subjectDao.querySubjectFullInfo();
		List<Map<String, Object>> chapters = this.chapterDao.queryChapterFullInfo();         //章
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		for (Profession p : pList) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", p.getId());
			map.put("text", p.getName());
			map.put("nodeType", "profession");
			map.put("state", "open");
			List<Map<String, Object>> children = new ArrayList<Map<String,Object>>();
			for (Map<String,Object> sub : subjects) {
				Map<String, Object> subMap = new HashMap<String, Object>();
				if (p.getId().toString().equals(sub.get("profession_id").toString())) {
					subMap.put("id", sub.get("id"));
					subMap.put("text", sub.get("name"));
					subMap.put("state", "open");
					subMap.put("nodeType", "subject");
					children.add(subMap);
				}
				List<Map<String, Object>> children1 = new ArrayList<Map<String,Object>>();
				for(Map<String,Object> c : chapters){
					if(sub.get("id").toString().equals(c.get("subject_id").toString())){
						Map<String, Object> chapterMap = new HashMap<String, Object>();
						chapterMap.put("id", c.get("id"));
						chapterMap.put("text", c.get("name"));
						chapterMap.put("state", "open");
						chapterMap.put("nodeType", "chapter");
						children1.add(chapterMap);
					}
				}
				subMap.put("children", children1);
			}
			map.put("children", children);
			lists.add(map);
		}
		return lists;
	}

	public List<Chapter> getBySubjectId(long subjectId) {
		// TODO Auto-generated method stub
		return this.chapterDao.getBySubjectId(subjectId);
	}
	
	
}
